package com.lyn.plugin_bluetooth;

import static android.bluetooth.le.AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY;
import static android.bluetooth.le.AdvertiseSettings.ADVERTISE_TX_POWER_HIGH;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.bluetooth.le.AdvertiseCallback;
import android.bluetooth.le.AdvertiseData;
import android.bluetooth.le.AdvertiseSettings;
import android.bluetooth.le.BluetoothLeAdvertiser;
import android.content.Context;
import android.content.Intent;
import android.os.ParcelUuid;
import android.view.View;
import android.widget.Toast;
import com.lyn.plugin.utils.LogUtil;
import com.lyn.plugin.PluginHelper;
import com.lyn.plugin.annotation.PluginLauncher;
import com.lyn.plugin.entity.PluginParameter;
import com.lyn.plugin.interfaces.ProxyActivityInterface;
import com.lyn.plugin_bluetooth.databinding.ActivityMainBinding;


@PluginLauncher(pluginName = "测试蓝牙",logoId = R.drawable.buletooth)
public class MainActivity implements ProxyActivityInterface {
    private static final int ENABLE_BLUETOOTH = 1234;
    private static final int BLE_PAYLOAD_LENGTH = 11;
    private PluginParameter pluginParameter;
    private ActivityMainBinding binding;
    private BluetoothManager bluetoothManager;
    private BluetoothAdapter bluetoothAdapter;

    @Override
    public void onLifeCycle(String s, Object... objects) {
        LogUtil.e("回调方法:"+s);
        if (s.equals("onCreate")) {
            this.pluginParameter = (PluginParameter) objects[0];
            View view = PluginHelper.getView(pluginParameter, R.layout.activity_main);
            binding = ActivityMainBinding.bind(view);
            pluginParameter.getActivity().setContentView(view);
            init();
        }
        onOther(s,objects);
    }

    private void init() {
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (bluetoothAdapter == null) {
            toast("本机未找到蓝牙功能!");
        }
        if (!bluetoothAdapter.isEnabled()){
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            pluginParameter.getActivity().startActivityForResult(intent, ENABLE_BLUETOOTH);
        }else {
            initBlueTooth();
        }

    }


    private void initBlueTooth(){
        //获取蓝牙管理器
        bluetoothManager = (BluetoothManager) pluginParameter.getActivity()
                .getSystemService(Context.BLUETOOTH_SERVICE);
        bluetoothAdapter=bluetoothManager.getAdapter();
        BluetoothLeAdvertiser bluetoothLeAdvertiser = bluetoothAdapter.getBluetoothLeAdvertiser();
        if (!bluetoothAdapter.isMultipleAdvertisementSupported()){
            LogUtil.e("不支持多广告!");
            return;
        }
        //蓝牙广播设置构造器
        AdvertiseSettings advertiseSettings=new AdvertiseSettings.Builder()
                //广播模式低延迟
                .setAdvertiseMode(ADVERTISE_MODE_LOW_LATENCY)
                //设置可连接
                .setConnectable(true)
                //设置超时
                .setTimeout(0)
                //设置使用高 TX 功率电平做广播。这对应于广告包的最大可见范围
                .setTxPowerLevel(ADVERTISE_TX_POWER_HIGH)
                .build();

        AdvertiseData advertiseData= new AdvertiseData.Builder()
                .setIncludeDeviceName(true)
                .addServiceUuid(ParcelUuid.fromString("00007777-0000-1000-8000-00805f9b34fb"))
                .addManufacturerData(0x7777, new byte[]{0x07, 0x07})
                .build();
        bluetoothLeAdvertiser.startAdvertising(advertiseSettings, advertiseData, new AdvertiseCallback() {
            @Override
            public void onStartSuccess(AdvertiseSettings settingsInEffect) {
                super.onStartSuccess(settingsInEffect);
                int mode = settingsInEffect.getMode();
                int timeout = settingsInEffect.getTimeout();
                int txPowerLevel = settingsInEffect.getTxPowerLevel();
                LogUtil.e("onStartSuccess=>{mode:"+mode+"timeout:"+timeout+"txPowerLevel:"+txPowerLevel+"}");
            }

            @Override
            public void onStartFailure(int errorCode) {
                super.onStartFailure(errorCode);
                LogUtil.e("onStartFailure:"+errorCode);
            }
        });
    }

    private void onOther(String s, Object... objects){
        if (s.equals("onActivityResult")){
            int requestCode= (int) objects[0];
            if (requestCode==ENABLE_BLUETOOTH){
                toast("蓝牙已开启！");
                initBlueTooth();
            }else {
                toast("请开启蓝牙！");
            }
        }
    }


    private void toast(String msg) {
        Toast.makeText(pluginParameter.getActivity(), msg, Toast.LENGTH_LONG).show();
    }


}